package com.oceansoft.core.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.io.IOUtils;

/**
 * 资源文件处理
 * 
 * @author Administrator
 * 
 */
public final class ResourceBundleUtil {

	/**
	 * 加载Properties
	 * 
	 * @param fileName
	 *            文件路径名
	 * @return
	 * @throws IOException
	 */
	public static Properties loadProperties(String fileName) throws IOException {
		return loadProperties(new File(fileName));
	}

	/**
	 * 静默加载Properties，如果没有加载到指定配置文件则返空文件
	 * 
	 * @param fileName
	 *            文件路径名
	 * @return
	 */
	public static Properties loadPropertiesQuietly(String fileName) {
		return loadPropertiesQuietly(new File(fileName));
	}

	public static Properties loadProperties(URL propertiesUrl) throws IOException {
		InputStream inputStream = null;
		try {
			inputStream = propertiesUrl.openStream();
			Properties properties = loadProperties(inputStream);
			return properties;
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
	}

	public static Properties loadPropertiesQuietly(URL propertiesUrl) {
		InputStream inputStream = null;
		Properties localProperties1;
		try {
			inputStream = propertiesUrl.openStream();
			Properties properties = loadProperties(inputStream);
			localProperties1 = properties;
		} catch (Exception e) {
			localProperties1 = new Properties();
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
		return localProperties1;
	}

	public static Properties loadProperties(File targetFile) throws IOException {
		FileInputStream input = null;
		Properties localProperties1;
		try {
			input = new FileInputStream(targetFile);
			Properties properties = loadProperties(input);
			localProperties1 = properties;
		} catch (FileNotFoundException e) {
			localProperties1 = new Properties();
		} finally {
			IOUtils.closeQuietly(input);
		}
		return localProperties1;
	}

	public static Properties loadPropertiesQuietly(File targetFile) {
		InputStream inputStream = null;
		Properties localProperties1;
		try {
			inputStream = new FileInputStream(targetFile);
			Properties properties = loadProperties(inputStream);
			localProperties1 = properties;
		} catch (Exception e) {
			localProperties1 = new Properties();
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
		return localProperties1;
	}

	public static Properties loadProperties(InputStream inputStream) throws IOException {
		Properties properties = new Properties();
		properties.load(inputStream);
		return properties;
	}

	public static Properties loadPropertiesQuietly(InputStream inputStream) {
		Properties properties = new Properties();
		try {
			properties.load(inputStream);
		} catch (IOException e) {
		}
		return properties;
	}

	/**
	 * 保存属性到Properties文件中
	 * @param properties
	 * @param targetFile
	 * @throws IOException
	 */
	public static void saveProperties(Properties properties, File targetFile) throws IOException {
		OutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(targetFile);
			saveProperties(properties, outputStream);
		} finally {
			IOUtils.closeQuietly(outputStream);
		}
	}

	public static void savePropertiesQuietly(Properties properties, File targetFile) {
		FileOutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(targetFile);
			saveProperties(properties, outputStream);
		} catch (IOException e) {
		} finally {
			IOUtils.closeQuietly(outputStream);
		}
	}

	public static void saveProperties(Properties properties, OutputStream outputStream) throws IOException {
		properties.store(outputStream, "####save by [ResourceBundleUtil]####");
	}

	public static void savePropertiesQuietly(Properties properties, OutputStream outputStream) {
		try {
			properties.store(outputStream, "####save by [ResourceBundleUtil]####");
		} catch (Exception e) {
		}
	}
}
